package com.yanqu.road.server.manager.activity.suitang.manager;

import com.yanqu.road.entity.activity.suitang.config.SuiTangCityTypeConfig;
import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangCity;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionJoinData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUserData;
import com.yanqu.road.entity.activity.suitang.entity.barracks.CrossSuiTangBarracks;
import com.yanqu.road.entity.activity.suitang.entity.battle.CrossSuiTangBattleResult;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeam;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangCommanderData;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionRound;
import com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserRound;
import com.yanqu.road.entity.log.cross.suitang.*;
import com.yanqu.road.server.manager.log.CrossAutoLogMgr;

import java.util.ArrayList;

public class CrossSuiTangLogActivityMgr extends CrossBaseActivityMgr {

    /*
     * 增加战略日志
     */
    public void addCommandLog(Long userId, String unionId) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        CrossSuiTangUnionData unionData = getUnionData(unionId);
        if (unionData == null) {
            return;
        }
        CrossSuiTangCommanderData commanders = unionData.getCommanders();
        CrossSuiTangUnionRound roundData = getUnionRoundData(unionId);
        int warType = 0;
        int homeId = 0;
        int actGroupId = 0;
        if (roundData != null) {
            warType = roundData.getWarType().getType();
            homeId = roundData.getHomeId();
            actGroupId = roundData.getActGroupId();
        }
        CrossAutoLogMgr.add(new LogCrossSuiTangCommand(config.getActivityId(), activity.getGroupId(), warType, actGroupId, homeId, unionData.getUnionUid(), userId, commanders.getMajorCityId(), commanders.getMinorCityId()));
    }

    /*
     * 增加指挥官任命日志
     */
    public void addCommandSetLog(String unionId) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        CrossSuiTangUnionData unionData = getUnionData(unionId);
        if (unionData == null) {
            return;
        }
        CrossSuiTangCommanderData commanders = unionData.getCommanders();
        if (commanders == null || commanders.getUserId().size() < 2) {
            return;
        }

        CrossAutoLogMgr.add(new LogCrossSuiTangCommanderSet(config.getActivityId(), unionId, commanders.getUserId().get(0), commanders.getUserId().get(1)));
    }

    /*
     * 增加分组日志
     */
    public void addGroupLog() {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        for (CrossSuiTangUnionJoinData unionJoin : activity.getUnionJoinDataMap().values()) {
            CrossSuiTangUnionRound unionRoundData = getUnionRoundData(unionJoin.getUnionUid());
            if (unionRoundData == null || unionJoin == null) {
                continue;
            }
            CrossAutoLogMgr.add(new LogCrossSuiTangGroup(config.getActivityId(), activity.getGroupId(), unionJoin.getUnionUid(),
                    unionRoundData.getWarType().getType(), unionRoundData.getActGroupId(), unionRoundData.getHomeId(),
                    unionJoin.getUnionBaseInfo().getTotalEarnSpeed().longValue()));
        }
    }

    /*
     * 增加锁定日志
     */
    public void addLockLog() {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        if (!activity.getCrossSuiTangData().getLockUnion()) {
            return;
        }
        for (CrossSuiTangUnionJoinData unionJoin : activity.getUnionJoinDataMap().values()) {
            CrossSuiTangUnionRound unionRoundData = getUnionRoundData(unionJoin.getUnionUid());
            if (unionRoundData == null || unionJoin == null) {
                continue;
            }
            CrossAutoLogMgr.add(new LogCrossSuiTangLock(config.getActivityId(), activity.getGroupId(), unionJoin.getUnionUid(), new ArrayList<>(unionJoin.getMemberMap().keySet()), unionJoin.getUnionBaseInfo().getTotalEarnSpeed().longValue()));
        }
    }

    /*
     * 增加成员加入锁定商会日志
     */
    public void addLockJoinLog(String unionId, long userId) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        if (!activity.getCrossSuiTangData().getLockUnion()) {
            return;
        }
        CrossAutoLogMgr.add(new LogCrossSuiTangLockJoin(config.getActivityId(), activity.getGroupId(), unionId, userId));
    }

    /*
     * 增加民心日志
     */
    public void addMinXinLog(String unionId, long addMinXin) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        CrossSuiTangUnionData unionData = getUnionData(unionId);
        CrossSuiTangUnionRound unionRoundData = getUnionRoundData(unionId);
        if (unionData == null || unionRoundData == null) {
            return;
        }
        CrossAutoLogMgr.add(new LogCrossSuiTangMinXin(config.getActivityId(), activity.getGroupId(), unionRoundData.getWarType().getType(),
                unionRoundData.getActGroupId(), unionRoundData.getHomeId(), unionId, addMinXin, unionData.getRedPacketValue()));
    }

    /*
     * 增加军资征收日志
     */
    public void addJunZiLog(long userId, int cityId, long junZi) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        if (activity.getNpcTestActivityMgr().isNpc(userId)) {
            return;
        }
        CrossSuiTangUserRound curUserRound = getCurUserRound(userId);
        if (curUserRound == null) {
            return;
        }
        CrossAutoLogMgr.add(new LogCrossSuiTangJunZi(config.getActivityId(), activity.getGroupId(), curUserRound.getWarType().getType(), curUserRound.getActGroupId(), userId, cityId, junZi));
    }

    /*
     * 同步战力日志
     */
    public void addSyncAbilityLog(long userId, long beforeAbility) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        if (activity.getNpcTestActivityMgr().isNpc(userId)) {
            return;
        }
        CrossSuiTangUserRound curUserRound = getCurUserRound(userId);
        CrossSuiTangTeam team = getTeam(userId);
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (curUserRound == null || team == null || userData == null) {
            return;
        }
        CrossAutoLogMgr.add(new LogCrossSuiTangSyncAbility(config.getActivityId(), activity.getGroupId(), curUserRound.getWarType().getType(),
                curUserRound.getActGroupId(), userId, beforeAbility, userData.getPatronsTotalAbility(), team.getMaxSoldier()));
    }

    /*
     * 增加占领日志
     */
    public void addOccupyLog(CrossSuiTangBattleResult result, CrossSuiTangCity city, int type) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }

        CrossSuiTangTeam ackTeam = getTeam(result.getAtk().getTeamNo());
        if (city == null || ackTeam == null) {
            return;
        }

        SuiTangCityTypeConfig cityTypeConfig = getCityTypeConfig(result.getCity());
        int unionPoint = cityTypeConfig == null ? 0 : cityTypeConfig.getCampPoint(calcRound() - 1);

        CrossAutoLogMgr.add(new LogCrossSuiTangOccupy(config.getActivityId(), activity.getGroupId(), city.getWarType().getType(),
                city.getActGroupId(), result.getAtk().getTeamNo(), ackTeam.getSoldierNum(), result.getDef().getTeamNo(),
                result.getDef().getSoldierNum(), city.getCityId(), type, getBuff(ackTeam.getUserId()), (int) result.getAtkHpReduce(),
                (int) result.getAtkZhanGong(), (int) result.getDefHpReduce(), (int) result.getDefZhanGong(), unionPoint));
    }

    /*
     * 增加兵营日志
     */
    public void addBarracksLog(long userId, long soldierAdd, int type) {
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        if (activity.getNpcTestActivityMgr().isNpc(userId)) {
            return;
        }
        CrossSuiTangUserRound curUserRound = getCurUserRound(userId);
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (curUserRound == null || userData == null) {
            return;
        }
        CrossSuiTangBarracks barracks = userData.getBarracks();
        if (barracks == null) {
            return;
        }

        CrossAutoLogMgr.add(new LogCrossSuiTangBarracks(config.getActivityId(), activity.getGroupId(), curUserRound.getWarType().getType(), curUserRound.getActGroupId(), userId, (int) barracks.getCreateSpeed(), type, soldierAdd, barracks.getReadyNum()));
    }
}
